﻿#Region "License & Usage"

'Microsoft Public License (Ms-PL) 

'This license governs use of the accompanying software. If you use the 
'software, you accept this license. If you do not accept the license, do 
'not use the software. 

'1. Definitions 

'The terms "reproduce," "reproduction," "derivative works," and 
'"distribution" have the same meaning here as under U.S. copyright law. 

'A "contribution" is the original software, or any additions or changes 
'to the software. 

'A "contributor" is any person that distributes its contribution under 
'this license. 

'"Licensed patents" are a contributor's patent claims that read directly 
'on its contribution. 

'2. Grant of Rights 

'(A) Copyright Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free copyright license to 
'reproduce its contribution, prepare derivative works of its 
'contribution, and distribute its contribution or any derivative works 
'that you create. 

'(B) Patent Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free license under its licensed 
'patents to make, have made, use, sell, offer for sale, import, and/or 
'otherwise dispose of its contribution in the software or derivative 
'works of the contribution in the software. 

'3. Conditions and Limitations 

'(A) No Trademark License- This license does not grant you rights to use 
'any contributors' name, logo, or trademarks. 

'(B) If you bring a patent claim against any contributor over patents 
'that you claim are infringed by the software, your patent license from 
'such contributor to the software ends automatically. 

'(C) If you distribute any portion of the software, you must retain all 
'copyright, patent, trademark, and attribution notices that are present 
'in the software. 

'(D) If you distribute any portion of the software in source code form, 
'you may do so only under this license by including a complete copy of 
'this license with your distribution. If you distribute any portion of 
'the software in compiled or object code form, you may only do so under a 
'license that complies with this license. 

'(E) The software is licensed "as-is." You bear the risk of using it. The 
'contributors give no express warranties, guarantees or conditions. You 
'may have additional consumer rights under your local laws which this 
'license cannot change. To the extent permitted under your local laws, 
'the contributors exclude the implied warranties of merchantability, 
'fitness for a particular purpose and non-infringement.

#End Region

#Region "Options"

Option Strict On
Option Explicit On

#End Region

#Region "Imports"

'From .Net Framework 3.5
Imports Microsoft.Drawing
Imports System.Drawing
Imports System.IO
Imports System.Reflection
Imports System.Windows.Forms

'From Custom Classes/Reference
Imports StedySoft.SenseSDK
Imports StedySoft.SenseSDK.DrawingCE
Imports SenseSDKExtended.DrawingCEEx

#End Region

Namespace DrawingCEEx.SenseAPIsEx.SenseNumericBox

    <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
    Friend Class HTCStyle2Modal
        Inherits StedySoft.SenseSDK.ITranslucentForm

        ''' <summary>
        ''' Check if it is VGA or QVGA
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _isVGA() As Boolean

            Try

                'Return the output
                Return StedySoft.SenseSDK.DrawingCE.Resolution.ScreenIsVGA

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Function

        ''' <summary>
        ''' _getIImageFromResource
        ''' </summary>
        ''' <param name="resource"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _getIImageFromResource(ByVal resource As String) As IImage
            Dim iimg As IImage = Nothing
            Using strm As MemoryStream = If(_isVGA, SenseSDKExtended.Resources.VGA.Images.GetIImageFromResource(resource), SenseSDKExtended.Resources.QVGA.Images.GetIImageFromResource(resource))
                ImagingFactory.GetImaging.CreateImageFromBuffer(strm.GetBuffer, Convert.ToUInt32(strm.Length), BufferDisposalFlag.BufferDisposalFlagNone, iimg)
            End Using
            Return iimg
        End Function

        Private _titleText As String = Nothing

        Private _topImage As IImage = _getIImageFromResource("numericbox.top")
        Private _midImage As IImage = _getIImageFromResource("numericbox.middle")
        Private _botImage As IImage = _getIImageFromResource("numericbox.bottom")
        Private _backImage As IImage = _getIImageFromResource("numericbox.3.digit.back.large")

        Private _upButtonImage As IImage = _getIImageFromResource("numericbox.up.large")
        Private _downButtonImage As IImage = _getIImageFromResource("numericbox.down.large")

        Private _upButtonPressedImage As IImage = _getIImageFromResource("numericbox.up.large.pressed")
        Private _downButtonPressedImage As IImage = _getIImageFromResource("numericbox.down.large.pressed")

        Private _topImageRectangle As Rectangle = Nothing
        Private _midImageRectangle As Rectangle = Nothing
        Private _botImageRectangle As Rectangle = Nothing
        Private _backImageRectangle As Rectangle = Nothing

        Private _titleTextRectangle As Rectangle = Nothing

        Private _upButtonRectabgle As Rectangle = Nothing
        Private _downButtonRectabgle As Rectangle = Nothing
        Private _okButtonRectangle As Rectangle = Nothing
        Private _cancelButtonRectangle As Rectangle = Nothing

        Private _value As Integer = Nothing
        Private _valueMinimum As Integer = Nothing
        Private _valueMaximum As Integer = Nothing
        Private _valueRectangle As Rectangle = Nothing

        'General Flags
        Private _clickedUP As Boolean = False
        Private _clickedDW As Boolean = False

        Private components As System.ComponentModel.IContainer
        Friend WithEvents value_timer As System.Windows.Forms.Timer = New System.Windows.Forms.Timer
        Private _digitPanel As DigitPanel_3 = New DigitPanel_3

        Private Sub SenseInputBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

            _topImageRectangle = New Rectangle(CInt((Screen.PrimaryScreen.Bounds.Width - SenseAPIs.SenseImage.GetIImageSize(_topImage).Width) / 2), ClientRectangle.Top + If(_isVGA, 20, 10), SenseAPIs.SenseImage.GetIImageSize(_topImage).Width, SenseAPIs.SenseImage.GetIImageSize(_topImage).Height)
            _midImageRectangle = New Rectangle(CInt((Screen.PrimaryScreen.Bounds.Width - SenseAPIs.SenseImage.GetIImageSize(_midImage).Width) / 2), _topImageRectangle.Top + _topImageRectangle.Height, SenseAPIs.SenseImage.GetIImageSize(_midImage).Width, If(_isVGA, 188, 94))
            _botImageRectangle = New Rectangle(CInt((Screen.PrimaryScreen.Bounds.Width - SenseAPIs.SenseImage.GetIImageSize(_botImage).Width) / 2), _midImageRectangle.Top + _midImageRectangle.Height, SenseAPIs.SenseImage.GetIImageSize(_botImage).Width, SenseAPIs.SenseImage.GetIImageSize(_botImage).Height)
            _backImageRectangle = New Rectangle(CInt((Screen.PrimaryScreen.Bounds.Width - SenseAPIs.SenseImage.GetIImageSize(_backImage).Width) / 2), _midImageRectangle.Top + If(_isVGA, 10, 5), SenseAPIs.SenseImage.GetIImageSize(_backImage).Width, SenseAPIs.SenseImage.GetIImageSize(_backImage).Height)

            _titleTextRectangle = New Rectangle(_topImageRectangle.Left + If(_isVGA, 40, 20), _topImageRectangle.Top + If(_isVGA, 8, 4), SenseAPIs.SenseImage.GetIImageSize(_topImage).Width - If(_isVGA, 20, 10), SenseAPIs.SenseImage.GetIImageSize(_topImage).Height)
            _valueRectangle = New Rectangle(_backImageRectangle.Left + If(_isVGA, 20, 10), _backImageRectangle.Top + If(_isVGA, 38, 19), If(_isVGA, 120, 60), If(_isVGA, 100, 50))

            _upButtonRectabgle = New Rectangle(_backImageRectangle.Left + If(_isVGA, 163, 81), _backImageRectangle.Top + If(_isVGA, 11, 5), SenseAPIs.SenseImage.GetIImageSize(_upButtonImage).Width, SenseAPIs.SenseImage.GetIImageSize(_upButtonImage).Height)
            _downButtonRectabgle = New Rectangle(_backImageRectangle.Left + If(_isVGA, 163, 81), _backImageRectangle.Top + If(_isVGA, 83, 41), SenseAPIs.SenseImage.GetIImageSize(_downButtonImage).Width, SenseAPIs.SenseImage.GetIImageSize(_downButtonImage).Height + If(_isVGA, 1, 1))

            _okButtonRectangle = New Rectangle(CInt((Screen.PrimaryScreen.Bounds.Width - _botImageRectangle.Width) / 2) + CInt(((_botImageRectangle.Width / 2) - If(_isVGA, 174, 87)) / 2) + If(_isVGA, 8, 4), (_botImageRectangle.Top - If(_isVGA, 8, 4)) + CInt((_botImageRectangle.Height - If(_isVGA, 79, 40)) / 2), If(_isVGA, 174, 87), If(_isVGA, 79, 40))
            _cancelButtonRectangle = New Rectangle(Screen.PrimaryScreen.Bounds.Width - _okButtonRectangle.Left - _okButtonRectangle.Width, (_botImageRectangle.Top - If(_isVGA, 8, 4)) + CInt((_botImageRectangle.Height - If(_isVGA, 79, 40)) / 2), If(_isVGA, 174, 87), If(_isVGA, 79, 40))

            Dim _up = New SenseButtonControlEx.HTCButtonStyle1
            _up.Bounds = _upButtonRectabgle
            _up.ButtonEnabledImage = _upButtonImage
            _up.ButtonPressedImage = _upButtonPressedImage
            AddHandler _up.OnClick, AddressOf _up_OnClick
            AddHandler _up.MouseDown, AddressOf _up_MouseDown
            AddHandler _up.MouseUp, AddressOf _up_MouseUp
            Me.Controls.Add(_up)

            Dim _down = New SenseButtonControlEx.HTCButtonStyle1
            _down.Bounds = _downButtonRectabgle
            _down.ButtonEnabledImage = _downButtonImage
            _down.ButtonPressedImage = _downButtonPressedImage
            AddHandler _down.OnClick, AddressOf _down_OnClick
            AddHandler _down.MouseDown, AddressOf _down_MouseDown
            AddHandler _down.MouseUp, AddressOf _down_MouseUp
            Me.Controls.Add(_down)

            Dim _ok = New SenseButtonControlEx.HTCButtonStyle1
            _ok.Name = "_ok"
            _ok.Text = "Ok"
            _ok.Left = _okButtonRectangle.Left
            _ok.Top = _okButtonRectangle.Top
            _ok.Width = _okButtonRectangle.Width
            _ok.Height = _okButtonRectangle.Height
            _ok.BackColor = Color.White
            AddHandler _ok.OnClick, AddressOf _ok_OnClick
            Me.Controls.Add(_ok)

            Dim _cancel = New SenseButtonControlEx.HTCButtonStyle1
            _cancel.Name = "_cancel"
            _cancel.Text = "Cancel"
            _cancel.Left = _cancelButtonRectangle.Left
            _cancel.Top = _cancelButtonRectangle.Top
            _cancel.Width = _cancelButtonRectangle.Width
            _cancel.Height = _cancelButtonRectangle.Height
            _cancel.BackColor = Color.White
            AddHandler _cancel.OnClick, AddressOf _cancel_OnClick
            Me.Controls.Add(_cancel)

            _digitPanel.Value = _value
            _digitPanel.Bounds = _valueRectangle
            _digitPanel.BackColor = Color.White
            Me.Controls.Add(_digitPanel)

        End Sub

        Private Sub _up_OnClick(ByVal sender As Object)

            If _digitPanel.Value = _valueMaximum Then
                _digitPanel.Value = _valueMinimum
            Else
                _digitPanel.Value += 1
            End If
            _digitPanel.Refresh()

        End Sub

        Private Sub _up_MouseUp(ByVal sender As Object, ByVal e As System.EventArgs)

            _clickedUP = False
            value_timer.Enabled = False

        End Sub

        Private Sub _up_MouseDown(ByVal sender As Object, ByVal e As System.EventArgs)

            _clickedUP = True
            value_timer.Enabled = True

        End Sub

        Private Sub _down_OnClick(ByVal sender As Object)

            If _digitPanel.Value = _valueMinimum Then
                _digitPanel.Value = _valueMaximum
            Else
                _digitPanel.Value -= 1
            End If
            _digitPanel.Refresh()

        End Sub

        Private Sub _down_MouseUp(ByVal sender As Object, ByVal e As System.EventArgs)

            _clickedDW = False
            value_timer.Enabled = False

        End Sub

        Private Sub _down_MouseDown(ByVal sender As Object, ByVal e As System.EventArgs)

            _clickedDW = True
            value_timer.Enabled = True

        End Sub

        Private Sub _ok_OnClick(ByVal sender As Object)

            Value = _digitPanel.Value
            Me.Close()

        End Sub

        Private Sub _cancel_OnClick(ByVal sender As Object)

            Me.Close()

        End Sub

        ''' <summary>
        ''' value_timer_Tick
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub value_timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles value_timer.Tick

            If _clickedUP Then

                If _digitPanel.Value = _valueMaximum Then
                    _digitPanel.Value = _valueMinimum
                Else
                    _digitPanel.Value += 1
                End If

            Else

                If _digitPanel.Value = _valueMinimum Then
                    _digitPanel.Value = _valueMaximum
                Else
                    _digitPanel.Value -= 1
                End If

            End If

            _digitPanel.Refresh()

        End Sub

        Private Sub SenseInputBox_OnRender(ByVal g As System.Drawing.Graphics) Handles MyBase.OnRender

            g.DrawImageAlphaChannel(_topImage, New Rectangle(_topImageRectangle.Left, _topImageRectangle.Top, _topImageRectangle.Width, _topImageRectangle.Height))

            g.DrawImageAlphaChannel(_midImage, New Rectangle(_midImageRectangle.Left, _midImageRectangle.Top, _midImageRectangle.Width, _midImageRectangle.Height))

            g.DrawImageAlphaChannel(_botImage, New Rectangle(_botImageRectangle.Left, _botImageRectangle.Top, _botImageRectangle.Width, _botImageRectangle.Height))

            g.DrawImageAlphaChannel(_backImage, New Rectangle(_backImageRectangle.Left, _backImageRectangle.Top, _backImageRectangle.Width, _backImageRectangle.Height))

            'Draw the title text with other properties like height,color,style,alignment,etc
            Using f As Font = New Font("Tahoma", 9, FontStyle.Regular)
                Dim s As String = SenseAPIsEx.SenseStrings.GetTrimmedDottedText(_titleText, "Tahoma", 9, FontStyle.Regular, New Size(_topImageRectangle.Width, _topImageRectangle.Height), g)
                Dim r As Rectangle = New Rectangle(_titleTextRectangle.Left, _titleTextRectangle.Top, _titleTextRectangle.Width, _titleTextRectangle.Height) '(Left,Top,Width,Height)
                Using b As Brush = New SolidBrush(Color.White)
                    Using sf As StringFormat = New StringFormat()
                        sf.FormatFlags = StringFormatFlags.NoWrap
                        sf.Alignment = StringAlignment.Near
                        sf.LineAlignment = StringAlignment.Center
                        g.DrawString(s, f, b, r, sf)
                    End Using
                End Using
            End Using

        End Sub

        Public Property Title() As String
            Get
                Return _titleText
            End Get
            Set(ByVal value As String)
                _titleText = value
            End Set
        End Property

        ''' <summary>
        ''' Value
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Value() As Integer
            Get
                Return _value
            End Get
            Set(ByVal value As Integer)
                _value = value
            End Set
        End Property

        ''' <summary>
        ''' Minimum
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Minimum() As Integer
            Get
                Return _valueMinimum
            End Get
            Set(ByVal value As Integer)
                _valueMinimum = value
            End Set
        End Property

        ''' <summary>
        ''' Maximum
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Maximum() As Integer
            Get
                Return _valueMaximum
            End Get
            Set(ByVal value As Integer)
                _valueMaximum = value
            End Set
        End Property

        Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container
            Me.value_timer = New System.Windows.Forms.Timer
            Me.SuspendLayout()
            '
            'value_timer
            '
            Me.value_timer.Interval = 1000
            '
            'HTCStyle1Modal
            '
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit
            Me.ClientSize = New System.Drawing.Size(240, 294)
            Me.Name = "HTCStyle1Modal"
            Me.ResumeLayout(False)

        End Sub

    End Class


    ''' <summary>DigitPanel_2</summary>
    ''' <author>Greenhacks</author>
    ''' <author-reference>None</author-reference>
    ''' <copyright>None</copyright>
    ''' <version>0.1</version>
    ''' <remarks>None</remarks>
    ''' <reference>None</reference>
    <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
    Friend Class DigitPanel_3
        Inherits System.Windows.Forms.UserControl

        'UserControl overrides dispose to clean up the component list.
        ''' <summary>
        ''' Dispose
        ''' </summary>
        ''' <param name="disposing"></param>
        ''' <remarks></remarks>
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private components As System.ComponentModel.IContainer

        Private _valueText As Integer = Nothing
        Private _valueTextRectangle As Rectangle = New Rectangle(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width, ClientRectangle.Height)
        Private _valueTextFontName As String = "Tahoma" '"Helvetica Neue OTS"
        Private _valueTextFontSize As Single = 28.0F
        Private _valueTextFontStyle As System.Drawing.FontStyle = FontStyle.Regular
        Private _valueTextForeColor As System.Drawing.Color = Color.Black
        Private _valueTextAlignment As System.Drawing.StringAlignment = StringAlignment.Near

        ''' <summary>
        ''' DigitPanel_2_Paint
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub DigitPanel_3_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

            'Draw the Value text with other properties like height,color,style,alignment,etc
            Using f As Font = New Font(_valueTextFontName, _valueTextFontSize, _valueTextFontStyle)
                Dim s As String = _valueText.ToString.PadLeft(3, CChar("0"))
                Dim r As Rectangle = New Rectangle(_valueTextRectangle.Left, _valueTextRectangle.Top, _valueTextRectangle.Width, _valueTextRectangle.Height) '(Left,Top,Width,Height)
                Using b As Brush = New SolidBrush(_valueTextForeColor)
                    Using sf As StringFormat = New StringFormat()
                        sf.Alignment = _valueTextAlignment
                        sf.LineAlignment = StringAlignment.Near
                        e.Graphics.DrawString(s, f, b, r, sf)
                    End Using
                End Using
            End Using

        End Sub

        ''' <summary>
        ''' Value
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Value() As Integer
            Get
                Return _valueText
            End Get
            Set(ByVal value As Integer)
                _valueText = value
            End Set
        End Property

    End Class

End Namespace

